package com.chat.simplechat.controller;

import com.chat.simplechat.entity.LoginUser;
import com.chat.simplechat.jwt.TokenBuilder;
import com.chat.simplechat.response.Result;
import com.chat.simplechat.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author wangyabin
 * @date 2022/10/28 18:36
 */
@Controller
@RequestMapping("/")
public class LoginController {
    
    @Autowired
    private UserInfoService userInfoService;
    
    @GetMapping({"/login","/"})
    public String login(){
        return "/login/Login";
    }

    /**
     * 登录
     * @param loginUser
     * @return
     */
    @ResponseBody
    @PostMapping("/signIn")
    public Result signIn(@RequestBody LoginUser loginUser){
        String userId = UUID.randomUUID().toString().replace("-","");
        loginUser.setUserId(userId);
        String username = loginUser.getUsername();
        Map<String,String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("userName",username);
        userInfoService.registerBean(username,loginUser);
        Map<String,Object> customData = new HashMap<>();
        customData.put("username",username);
        String token = TokenBuilder.createToken(map);
        return Result.SUCCESS(token,customData);
    }
}
